<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateOrdersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // 下单后的数据不会受到编辑而被影响到, 所以能存的 都得存一份 (也可以理解成 冗余 一份用在查询), 属于历史数据
        Schema::create('orders', function (Blueprint $table) {
            $table->id();
            $table->char('numbering', 22)->unique()->comment('订单编号 (type 2位)(YmdHis 14位)(随机字符串 6位)');
            $table->string('remark')->default('')->comment('订单备注');
            $table->tinyInteger('status')->default(1)->comment('订单状态; 1:待发货 2:已发货 3:已取消');
            $table->bigInteger('status_cancel_operator_id')->default(0)->comment('操作员 ID');
            $table->tinyInteger('status_cancel_type')->default(1)->comment('订单取消类型; 1:代理商取消 2:后台取消');
            $table->timestamp('cancel_time')->nullable()->comment('取消时间');

            $table->unsignedBigInteger('original_total_amount')->default(0)->comment('[原始] 订单总金额 (单位 分,包含运费加商品总价)');
            $table->unsignedBigInteger('total_amount')->default(0)->comment('订单总金额 (单位 分,包含运费加商品总价)');

            $table->unsignedBigInteger('original_commodity_price')->default(0)->comment('[原始]商品总价格 (单位 分)');
            $table->unsignedBigInteger('original_commodity_balance_paid')->default(0)->comment('[原始]商品花费的可提现金额 (单位 分)');
            $table->unsignedBigInteger('original_commodity_unextractable_balance_paid')->default(0)->comment('[原始]花费的不可提现金额 (单位 分)');
            $table->unsignedBigInteger('original_commodity_amount')->default(0)->comment('[原始]商品总数量');
            $table->unsignedBigInteger('commodity_price')->default(0)->comment('商品总价格 (单位 分),收退货退款取消影响');
            $table->unsignedBigInteger('commodity_balance_paid')->default(0)->comment('商品花费的可提现金额 (单位 分),收退货退款取消影响');
            $table->unsignedBigInteger('commodity_unextractable_balance_paid')->default(0)->comment('花费的不可提现金额 (单位 分),收退货退款取消影响');
            $table->unsignedBigInteger('commodity_amount')->default(0)->comment('商品总数量,收退货退款取消影响');

            $table->unsignedInteger('total_number_of_returned')->default(0)->comment('总退货商品数量,受订单退货商品数量影响');
            $table->unsignedInteger('total_number_of_refunded')->default(0)->comment('总退款商品数量,受订单退款商品数量影响');

            $table->unsignedBigInteger('original_freight')->default(0)->comment('[原始]运费 (单位 分)');
            $table->unsignedBigInteger('original_freight_balance_paid')->default(0)->comment('[原始]运费花费的可提现金额 (单位 分)');
            $table->unsignedBigInteger('original_freight_unextractable_balance_paid')->default(0)->comment('[原始]运费花费的不可提现金额 (单位 分)');
            $table->unsignedBigInteger('freight')->default(0)->comment('运费 (单位 分),受取消影响');
            $table->unsignedBigInteger('freight_balance_paid')->default(0)->comment('运费花费的可提现金额 (单位 分),受取消影响');
            $table->unsignedBigInteger('freight_unextractable_balance_paid')->default(0)->comment('运费花费的不可提现金额 (单位 分),受取消影响');

            $table->bigInteger('agent_id')->index()->comment('代理商 ID');
            $table->text('agent_snapshot')->nullable()->comment('代理商快照');
            $table->bigInteger('partner_id')->default(0)->index()->comment('(代理商所属) 合伙人 ID');
            $table->unsignedBigInteger('original_income')->default(0)->comment('[原始]代理商收入 (单位 分)');
            $table->unsignedBigInteger('income')->default(0)->comment('代理商收入 (单位 分),收退货退款取消影响');

            $table->unsignedBigInteger('direct_inviter')->index()->comment('直接邀请人 ID');
            $table->text('direct_inviter_snapshot')->nullable()->comment('直接邀请人快照');
            $table->bigInteger('indirect_inviter')->index()->comment('间接邀请人 ID');
            $table->text('indirect_inviter_snapshot')->nullable()->comment('间接邀请人快照');

            $table->bigInteger('commodity_id')->comment('商品 ID');
            $table->string('commodity_name')->default('')->comment('商品名称');
            $table->string('commodity_numbering')->default('')->comment('商品编号');
            $table->text('commodity_snapshot')->nullable()->comment('商品数据快照');

            $table->bigInteger('commodity_activity_id')->comment('商品活动 ID');
            $table->text('commodity_activity_snapshot')->nullable()->comment('商品活动数据快照');

            $table->text('reward_setting_snapshot')->nullable()->comment('下单奖励设置快照');

            // 下单时的 买家信息
            $table->bigInteger('client_id')->comment('买家 ID');
            $table->string('client_name')->default('')->comment('买家名称');
            $table->char('client_phone', 11)->index()->default('')->comment('买家手机号');
            $table->string('client_address')->default('')->comment('买家收货地址');
            $table->text('client_snapshot')->nullable()->comment('买家快照');

            $table->bigInteger('supplier_id')->comment('供应商 ID');
            $table->string('management_notes')->default('')->comment('管理备注');

            $table->tinyInteger('is_expired')->default(2)->comment('已失效(售后退完了); 1:是 2:否');
            $table->date('order_date')->index()->comment('下单日期');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('orders');
    }
}
